﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class AreaInfoDal
    {
         public DapperHelper dapperHelper { get; set; }


        /// <summary>
        /// 根据市查所属区县
        /// </summary>
        /// <param name="cityid">市ID</param>
        /// <returns></returns>
        public List<AreaInfoView> QueryAreaByCityId(Guid cityid)
        {
            List<AreaInfoView> areaInfoViews = null;
            SqlBuilder sqlBuilder = new SqlBuilder();
            DynamicParameters sqlParams = new DynamicParameters();

            dapperHelper.ExecueWithoutTransaction(a =>
            {
                string sql = @"select * from new_area /**where**/";
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("new_city=@new_city");
                sqlParams.Add("@new_city", cityid);
                areaInfoViews = a.Query<AreaInfoView>(sqlTemplate.RawSql, sqlParams).ToList();
            });

            return areaInfoViews;
        }
        /// <summary>
        /// 根据区域ID查询区域信息
        /// </summary>
        /// <param name="areaid">区域ID</param>
        /// <returns></returns>
        public AreaInfoView QueryAreaById(Guid areaid)
        {
            AreaInfoView areaInfoView = null;
            SqlBuilder sqlBuilder = new SqlBuilder();
            DynamicParameters sqlParams = new DynamicParameters();

            dapperHelper.ExecueWithoutTransaction(a =>
            {
                string sql = @"select * from new_area /**where**/";
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("new_areaid=@new_areaid");
                sqlParams.Add("@new_areaid", areaid);
                areaInfoView = a.QueryFirstOrDefault<AreaInfoView>(sqlTemplate.RawSql, sqlParams);
            });

            return areaInfoView;
        }
    }
}
